package com.qf.kady.ssm.service.impl;

import com.qf.kady.ssm.entity.ResultVO;
import com.qf.kady.ssm.mapper.DropLogMapper;
import com.qf.kady.ssm.mapper.ReaderMapper;
import com.qf.kady.ssm.pojo.DropLog;
import com.qf.kady.ssm.pojo.Reader;
import com.qf.kady.ssm.pojo.User;
import com.qf.kady.ssm.service.ReaderService;
import com.qf.kady.ssm.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/4/21
 * \* Time: 11:50
 * \* 千峰JAVA2101班
 * \* Description: 读者业务层
 * \
 */
@Service
public class ReaderServiceImpl implements ReaderService {

    @Autowired
    private ReaderMapper readerMapper;

    @Autowired
    private DropLogMapper dropLogMapper;

    @Override
    public ResultVO login(Reader reader) {
        String loginName = reader.getReaderName().trim();
        String password = reader.getPassword().trim();
        if(StringUtils.isEmpty(loginName)){
            return ResultVO.error("读者名不能为空！",null);
        }
        if(StringUtils.isEmpty(password)){
            return ResultVO.error("密码不能为空！",null);
        }
        Reader resReader  = readerMapper.selectReaderByCondition(null,loginName);
        if(ObjectUtils.isEmpty(resReader)){
            return ResultVO.error("该读者用户不存在",null);
        }
        if(!MD5Util.md5(password).equals(resReader.getPassword())){
            return ResultVO.error("密码错误",null);
        }
        return ResultVO.ok("登录成功",resReader);
    }

    @Override
    public List<Reader> getReaderListByCondition(String keyword) {
        List<Reader> list = readerMapper.getListByCondition(keyword);
        return list;
    }

    @Override
    public Reader getReaderDetailById(Long readerId) {
        Reader reader = readerMapper.selectReaderByCondition(readerId,null);
        return reader;
    }

    @Override
    public boolean updateReader(Reader reader) {
        boolean res = readerMapper.updateReaderById(reader);
        return res;
    }

    @Override
    public boolean addReader(Reader reader) {
        boolean res = readerMapper.addReader(reader);
        return res;
    }

    @Override
    public ResultVO deleteReader(Long readerId) {
        DropLog res = dropLogMapper.findByCondition(null, readerId);
        if(res!=null&&res.getBackDate()==null){
            return ResultVO.error("删除失败，该读者还有借阅的书品，无法删除！",null);
        }
        if(res!=null){
            dropLogMapper.deleteDroplog(res.getSerNum());
        }
        readerMapper.deleteReaderById(readerId);
        return ResultVO.ok("删除成功",null);
    }

    @Override
    public ResultVO changeWD(Long readerId, String oldPasswd, String newPasswd) {
        Reader reader = readerMapper.selectReaderByCondition(readerId,null);
        if(reader==null){
            return ResultVO.error("读者不存在",null);
        }
        if(!MD5Util.md5(oldPasswd).equals(reader.getPassword())){
            return ResultVO.error("原密码错误，请重试",null);
        }
        reader.setPassword(MD5Util.md5(newPasswd));
        boolean res = readerMapper.updateReaderById(reader);
        return ResultVO.ok("修改成功",null);
    }
}
